import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {HireDeviceService} from '../hireDevice.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {NzModalService} from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-commodity',
  templateUrl: './commodity.component.html',
  styleUrls: ['./commodity.component.scss']
})
export class CommodityComponent implements OnInit {
  query = {
    title: '',
    status: '',
    date: [],
    page: 1,
    pageSize: 10,
  };
  // 表格数据
  listOfData = {
    page: 1,
    total: 100,
    records: []
  };

  // 选择
  checked = false;
  indeterminate = false;
  setOfCheckedId = new Set<number>();

  constructor(
    private router: Router,
    private hireDeviceService: HireDeviceService,
    private nzMessageService: NzMessageService,
    private nzModalService: NzModalService,
  ) {
  }

  ngOnInit(): void {
    this.getTableData();
  }

  onAllChecked(checked: boolean): void {
    this.listOfData.records
      // @ts-ignore
      .filter(({disabled, status}) => !disabled && status !== 4)
      // @ts-ignore
      .forEach(({id}) => this.updateCheckedSet(id, checked));
    this.refreshCheckedStatus();
  }

  refreshCheckedStatus(): void {
    // @ts-ignore
    const listOfEnabledData = this.listOfData.records.filter(({disabled, status}) => !disabled && status !== 4);
    // @ts-ignore
    this.checked = listOfEnabledData.every(({id}) => this.setOfCheckedId.has(id));
    // @ts-ignore
    this.indeterminate = listOfEnabledData.some(({id}) => this.setOfCheckedId.has(id)) && !this.checked;
  }

  onItemChecked(id: number, checked: boolean): void {
    this.updateCheckedSet(id, checked);
    this.refreshCheckedStatus();
  }


  updateCheckedSet(id: number, checked: boolean): void {
    if (checked) {
      this.setOfCheckedId.add(id);
    } else {
      this.setOfCheckedId.delete(id);
    }
  }

  onPageIndexChange(index: number) {
    this.query.page = index;
    this.getTableData();
  }

  onPageSizeChange(pageSize: number) {
    this.query.pageSize = pageSize;
    this.getTableData();
  }

  getTableData() {
    this.checked = false;
    this.indeterminate = false;
    this.setOfCheckedId = new Set<number>();
    this.hireDeviceService.getCommodityList({
      title: this.query.title,
      status: this.query.status,
      updateBeginTime: this.hireDeviceService.formatTime(this.query.date[0], 'date'),
      updateEndTime: this.hireDeviceService.formatTime(this.query.date[1], 'date'),
      page: this.query.page,
      pageSize: this.query.pageSize,
    }).then((res: any) => {
      if (res.code === 0) {
        this.listOfData = res.data;
      }
    });
  }

  queryBtn() {
    this.query.page = 1;
    this.getTableData();
  }

  resetCondition() {
    this.query = {
      title: '',
      status: '',
      date: [],
      page: 1,
      pageSize: 10,
    };
    this.getTableData();
  }

  toEditCommodity(id?: any) {
    this.router.navigate(['hireDevice/hireDeviceCommodityEdit'], {queryParams: {id}});
  }

  deleteCommodity(id: any) {
    this.hireDeviceService.commodityDelete(id).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.getTableData();
      }
    });
  }

  batchOption(type: 'up' | 'down') {
    if (!this.setOfCheckedId.size) {
      this.nzMessageService.warning('请选择要操作的数据');
      return;
    }
    this.nzModalService.create({
      nzTitle: '提示',
      nzContent: `确认要${type === 'up' ? '上架' : '下架'}已选中数据？`,
      nzMaskClosable: false,
      nzClosable: false,
      nzOnOk: () => {
        this.hireDeviceService.commodityBatchStatus({
          list: Array.from(this.setOfCheckedId),
          status: type === 'up' ? 0 : 1
        }).then((res: any) => {
          if (res.code === 0) {
            this.nzMessageService.success('操作成功');
            this.getTableData();
          }
        });
      }
    });
  }

}
